package com.sunday.fangdaifu.activity.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.reflect.TypeToken;
import com.sunday.common.adapter.MainFragmentPagerAdapter;
import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.common.Api;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.DateTitle;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class DateFragment extends BaseFragment implements ApiServiceImpl.HttpResponseInterface {


    @Bind(R.id.slide_tab)
    SlidingTabLayout slideTab;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    public static DateFragment newInstance() {
        DateFragment fragment = new DateFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_date, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        ApiClient.getApiService().dateTitle(this, new TypeToken<ResultDO<List<DateTitle>>>() {
        }.getType());
    }
    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        switch (key) {
            case Api.DATE_TITLE:
                ResultDO<List<DateTitle>> resultDO = (ResultDO<List<DateTitle>>) result;
                List<DateTitle> dateTitles=resultDO.getResult();
                List<String> title=new ArrayList<>();
                title.clear();
                if (!dateTitles.isEmpty())
                for(DateTitle d:dateTitles){
                    String s= d.getName();
                    title.add(s);
                    fragments.add(DateChildFragment.newInstance(d.getId()));
                }
                viewPager.setAdapter(new MainFragmentPagerAdapter(getChildFragmentManager(),fragments,
                       title));
                slideTab.setViewPager(viewPager);
                viewPager.setOffscreenPageLimit(title.size());
                viewPager.setCurrentItem(0, false);
                break;

        }
    }

    @Override
    public void onFailure() {
        ToastUtils.showToast(mContext, "服务器异常");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
